package study

import (
	"fmt"
	"sync"
	"sync/atomic"
)

/*
atomic.AddUint32(addr *uint32, delta uint32) uint32
    atomic.AddUint64(addr *uint64, delta uint64) uint64
    atomic.AddInt32(addr *int32, delta int32) int32
    atomic.AddInt64(addr *int64, delta int64) int64
    atomic.AddUintptr(addr *uintptr, delta uintptr) uintptr
atomic.StoreUint32(addr *uint32, val uint32)
atomic.StoreUint64(addr *uint64, val uint64)
atomic.StoreInt32(addr *int32, val int32)
atomic.StoreInt64(addr *int64, val int64)
atomic.StoreUintptr(addr *uintptr, val uintptr)
atomic.StorePointer(addr *unsafe.Pointer, val unsafe.Pointer)
atomic.CompareAndSwapUint32(addr *uint32, old, new uint32) bool
atomic.CompareAndSwapUint64(addr *uint64, old, new uint64) bool
atomic.CompareAndSwapInt32(addr *int32, old, new int32) bool
atomic.CompareAndSwapInt64(addr *int64, old, new int64) bool
atomic.CompareAndSwapUintptr(addr *uintptr, old, new uintptr) bool
atomic.CompareAndSwapPointer(addr *unsafe.Pointer, old, new unsafe.Pointer) bool

*/

func main() {
	var sum uint32 = 100
	// var sum int32 = 100
	var wg sync.WaitGroup
	for i := 0; i < 50; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			//sum += 1                 //1
			atomic.AddUint32(&sum, 1) //2
			// atomic.AddInt32(&sum, -1)  //2
		}()
	}
	wg.Wait()
	fmt.Println(sum)
}
